
#include <iostream>

#include "Events.h"
#include "Message.h"
#include "PlatformFactory.h"
#include "Slave.h"
#include "Types.h"

Slave::Slave(std::string remote_address, int remote_port, MncPoint screen_size)
{
    messenger_ = new Messenger(remote_address, remote_port);
}

Slave::~Slave()
{
    delete messenger_;
}

void Slave::Start()
{
    //abyte buffer[sizeof(Message)];
    Message message = Message();
    Screen screen = PlatformFactory::GetScreen();
    screen_size_ = screen.GetSize();
    
    while (true)
        Process(&messenger_->Receive(message));
}

void Slave::Process(Message* message)
{
    //if(!be_slave_ && message->event_type == Message::kEventMouseEnter)
        HandleMouseEntered(message);
}

void Slave::HandleMouseEntered(Message* message)
{
    x_entry_ = (Xentry)message->x;
    
    //if(message->x == left)
    //    message->x = 0;
    //else
    //    message->x = screen_size_.x;
    
    // Translate from percentage of screen height to actual pixels
    //message->y = ((float)message->y / 100) * screen_size_.y;
    
    be_slave_ = true;
	EnactMouseEvent(Message::kEventMouseMove, MncPoint(message->x, message->y), message->coordinate_type);
}

bool Slave::MouseIsLeaving(MncPoint* location)
{
    //(_xEntry == left && mouseLoc.x == 0 && event.x < 0) || 
    //(_xEntry == right && mouseLoc.x == screenBounds.size.width && event.x > 0)
    return false;
}
